package com.example.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.common.Result;
import com.example.interceptor.SSOSessionData;
import com.example.interceptor.UserSessionHolder;
import com.example.model.bo.LoginInfo;
import com.example.service.LoginService;
import com.example.utils.PropertyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Objects;

/**
 * @author lzh
 * create 2024-11-20:04
 */

@RestController
@Slf4j
@CrossOrigin
public class AutoRegistController {
    // 1. 配置拦截器（我要把这个内容添加到拦截器中，
    // 2. 从拦截器获取username
    // 3.根据username自动注册
    // 4.注册好的账户进行登录
    @Autowired
    private LoginService loginService;

    @PostMapping("/autoregister")
    public Result sutoRegist () throws Exception {
        SSOSessionData currentUser = UserSessionHolder.getCurrentUser();
        String userName;
        if (currentUser != null) {
            userName = currentUser.getUsername();
        } else {
            userName = PropertyUtil.getUserName();
        }
        LoginInfo dto = new LoginInfo(userName, "12345678", true);
        Map<String, String> register_resp = loginService.register(dto);
        if (Objects.equals(register_resp.get("state"), "success")){
            return Result.success(register_resp.get("address"));
        }
        return Result.error(register_resp.get("state"));
    }

    @PostMapping("/UserApiService/inner/queryUserDetailByUserName")
    public JSONObject checkUser(@RequestBody Map<String, String> requestMap) throws Exception {
        JSONObject jsonObject = new JSONObject();
        // 构建响应
        JSONObject resultData = new JSONObject();
        resultData.put("respCode", 200);
        resultData.put("respMsg", "success");
        jsonObject.put("userId", "");
        resultData.put("user", jsonObject);
        return resultData;
    }


}
